/*
 * @(#) $Header$
 *
 * Copyright (C) 2013  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.baselib.util.iterator;

import java.util.Iterator;

/**
 * Class {@code ConstantIterator} returns an unlimited amount of time the same
 * value. One can wrap this iterator into a {@link RangedIterator} to return
 * a limited number of values.
 *
 * @param   <E>   the type of the value.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.baselib.util.iterator.ConstantIterator">Daniel Léonard</a>
 * @version $Revision$
 */
public class ConstantIterator<E> implements Iterator<E> {

//---------------------------
// Instance variables
//---------------------------

   /** The value. */
   private E value;


//---------------------------
// Constructors
//---------------------------

   /**
    * Constructor.
    * @param   value   the value.
    */
   public ConstantIterator(E value) {
      this.setValue(value);
      }


//---------------------------
// Accessors and mutators
//---------------------------

   /**
    * Gives the value.
    * @return   the value.
    */
   protected E getValue() {
      return this.value;
      }

   /**
    * Changes the value.
    * @param   value   the new value.
    */
   protected void setValue(E value) {
      this.value = value;
      }


//---------------------------
// Implemented methods from java.util.Iterator
//---------------------------

   /**
    * {@inheritDoc}
    */
   @Override
   public E next() {
      E e = this.getValue();
      return e;
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public boolean hasNext() {
      boolean has_next = true;
      return has_next;
      }

   /**
    * Unsupported operation.
    * @throws   UnsupportedOperationException   always.
    */
   @Override
   public void remove() {
      String message = this.getUnsupportedMethodErrorMessage("remove()"); //$NON-NLS-1$
      throw new UnsupportedOperationException(message);
      }


//---------------------------
// Class methods
//---------------------------

   /**
    * Creates a new constant iterator.
    * @param   value   the value.
    * @return  a new constant iterator.
    */
   public static <E> ConstantIterator<E> create(E value) {
      ConstantIterator<E> iterator = new ConstantIterator<>(value);
      return iterator;
      }


//---------------------------
// i18n methods
//---------------------------

   /**
    * Gets the error message telling that the given method is unsupported.
    * @param   method   the name of the unsupported method.
    * @return   the formatted message.
    */
   protected String getUnsupportedMethodErrorMessage(String method) {
      String key = Resources.ITERATOR_UNSUPPORTED_METHOD;
      String message = Resources.getLocalizedString(key, method);
      return message;
      }

   }
